package com.sdkj.recruit.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sdkj.recruit.dao.IUserDao;
import com.sdkj.recruit.dao.IUserLabelDao;
import com.sdkj.recruit.dao.IUserResumeDao;
import com.sdkj.recruit.dao.IWeixinConfigDao;
import com.sdkj.recruit.model.CompanyResume;
import com.sdkj.recruit.model.EducationalExperience;
import com.sdkj.recruit.model.ResultCode;
import com.sdkj.recruit.model.ResultData;
import com.sdkj.recruit.model.Resume;
import com.sdkj.recruit.model.SmsCode;
import com.sdkj.recruit.model.User;
import com.sdkj.recruit.model.UserLabel;
import com.sdkj.recruit.model.WeixinConfig;
import com.sdkj.recruit.model.WorkExperience;
import com.sdkj.recruit.service.IUserService;
import com.sdkj.recruit.util.RandomUtil;
import com.sdkj.recruit.util.StringUtil;
import com.sdkj.recruit.vo.CompanyVo;
import com.sdkj.recruit.vo.EducationalExperienceVo;
import com.sdkj.recruit.vo.MenuVo;
import com.sdkj.recruit.vo.ResumeVo;
import com.sdkj.recruit.vo.UserLabelVo;
import com.sdkj.recruit.vo.UserVo;
import com.sdkj.recruit.vo.WorkExperienceVo;

@Service("userService")
public class UserService implements IUserService {

	@Autowired
	private IUserDao userDao;

	@Autowired
	private Mapper mapper;

	@Autowired
	private IUserResumeDao resumeDao;

	@Autowired
	private IUserLabelDao lableDao;
	
	@Autowired
	private IWeixinConfigDao weixinConfigDao;
	
	
	@Override
	public UserVo login() {

		User user = userDao.querUser();

		if (user == null) {

			return null;
		}
		return mapper.map(user, UserVo.class);
	}

	@Override
	public User queryUserByName(String userName) {

		return userDao.queryUserByName(userName);
	}

	@Override
	public List<MenuVo> getUserMeuns(User user) {

		List<MenuVo> menus = new ArrayList<MenuVo>();

		if (user.getUserType() == 1 || user.getUserType() == 2) {

			MenuVo menu1 = new MenuVo();
			menu1.setMarkName("userManagement");
			menus.add(menu1);

			/*
			 * MenuVo menu2 = new MenuVo(); menu2.setMarkName("labelManagement");
			 * menus.add(menu2);
			 * 
			 * MenuVo menu3 = new MenuVo(); menu3.setMarkName("shareManagement");
			 * menus.add(menu3);
			 * 
			 * MenuVo menu4 = new MenuVo(); menu4.setMarkName("autoUpdateManagement");
			 * menus.add(menu4);
			 */

			/*
			 * MenuVo menu5 = new MenuVo(); menu5.setMarkName("merchantManagement");//
			 * 用商户管�? menus.add(menu5);
			 * 
			 * MenuVo menu6 = new MenuVo(); menu6.setMarkName("smallNoManagement");// 小号
			 * menus.add(menu6);
			 */
		}
		return menus;
	}

	@Override
	public void saveUsers(List<User> list) {

		if (CollectionUtils.isNotEmpty(list)) {

			for (User user : list) {
				userDao.saveUsers(user);
			}
		}
	}

	@Override
	public void deleteUserById(String userId) {
		userDao.deleteUserById(userId);
	}

	@Override
	public void cleanUserLoginInfo(String userId) {

		userDao.cleanUserLoginInfo(userId);
	}

	@Override
	public void updateUserLoginInfo(String accessToken, Date lastLoginTime, long expireTime, String userId) {
		userDao.updateUserLoginInfo(accessToken, lastLoginTime, expireTime, userId);
	}

	@Override
	public UserVo queryUserById(String userId) {

		User user = userDao.queryUserById(userId);
		if (user == null) {
			return null;
		}
		// 获取用户标签信息
		List<UserLabel> list =lableDao.queryUserLabelList(user.getUserId());
		UserVo vo =mapper.map(user, UserVo.class);
		
		if(!CollectionUtils.isEmpty(list)) {
			List<UserLabelVo> voList =new ArrayList<UserLabelVo>();
			for(UserLabel l :list) {
				UserLabelVo uvo =mapper.map(l, UserLabelVo.class);
				voList.add(uvo);
			}
			vo.setLabelList(voList);
		}
		return vo;
	}

	@Override
	public void updatePassword(String userId, String createNewpassword) {
		userDao.updatePassword(userId, createNewpassword);
	}

	@Override
	public User queryUserByPhone(String mobile) {
		return userDao.queryUserByPhone(mobile);
	}

	@Override
	public ResultData<Object> register(User user) {
		userDao.saveUsers(user);
		return ResultCode.success("新增用户成功", user);
	}

	@Override
	public void updateUserProperty(String userId, Map<String, Object> property) {
		userDao.updateUserProperty(userId, property);
	}

	@Override
	public List<UserVo> queryUserList(int pageNo, int pageSize, String keyWord) {
		List<UserVo> list = new ArrayList<UserVo>();
		List<User> userList = userDao.queryUserList(pageNo, pageSize, keyWord);
		if (CollectionUtils.isNotEmpty(userList)) {
			for (User user : userList) {
				UserVo vo = mapper.map(user, UserVo.class);
				list.add(vo);
			}
		}
		return list;
	}

	@Override
	public long queryAllUserCount(String keyWord) {
		return userDao.queryAllUserCount(keyWord);
	}

	@Override
	public void deleteUser(String userId) {
		userDao.deleteUserById(userId);
	}

	@Override
	public void updateUserByUserId(String userId, Map<String, Object> map) {
		userDao.updateUserProperty(userId, map);
	}

	@Override
	public void saveUser(UserVo userVo) {

	}

	@Override
	public User queryUserByToken(String token) {
		return userDao.queryUserByToken(token);
	}

	@Override
	public UserVo queryUserByEmCode(String emCode) {
		User user = userDao.queryUserByEmCode(emCode);
		if (user == null) {

			return null;
		}
		return mapper.map(user, UserVo.class);
	}

	@Override
	public void saveSmsInfo(SmsCode sms) {
		userDao.saveSmsInfo(sms);
	}

	@Override
	public SmsCode querySms(String mobile,int codeType) {
		return userDao.querySms(mobile,codeType);
	}

	@Override
	public ResultData<Object> saveResume(ResumeVo resumeVo) {

		Resume resume = mapper.map(resumeVo, Resume.class);
		resume.setResumeId(new RandomUtil().getRandomId());
		// 保存简历信息
		resumeDao.saveResume(resume);

		// 保存教育经验
		if (!CollectionUtils.isEmpty(resumeVo.getEduList())) {

			List<EducationalExperience> eduList = new ArrayList<EducationalExperience>();

			for (EducationalExperienceVo vo : resumeVo.getEduList()) {
				EducationalExperience ee = mapper.map(vo, EducationalExperience.class);
				ee.setId(new RandomUtil().getRandomId());
				ee.setResumeId(resume.getResumeId());
				eduList.add(ee);
			}
			resumeDao.saveEducationalExperience(eduList);
		}

		// 保存工作经验
		if (!CollectionUtils.isEmpty(resumeVo.getWorkList())) {

			List<WorkExperience> workList = new ArrayList<WorkExperience>();

			for (WorkExperienceVo vo : resumeVo.getWorkList()) {
				WorkExperience ee = mapper.map(vo, WorkExperience.class);
				ee.setId(new RandomUtil().getRandomId());
				ee.setResumeId(resume.getResumeId());
				workList.add(ee);
			}

			resumeDao.saveWorkExperience(workList);
		}
		return ResultCode.success("保存简历成功", resume);
	}

	@Override
	public ResultData<Object> updateResume(ResumeVo resumeVo) {

		Resume resume = mapper.map(resumeVo, Resume.class);

		// 保存简历信息
		resumeDao.updateResume(resume);
		
		return ResultCode.success("修改简历成功", resume);

	}

	@Override
	public ResultData<Object> deleteResume(String resumeId) {

		resumeDao.deleteResume(resumeId);

		resumeDao.deleteWorkList(resumeId);

		resumeDao.deleteEduList(resumeId);

		return ResultCode.success("删除简历成功", resumeId);
	}

	@Override
	public ResultData<Object> queryResume(String resumeId) {

		// 简历基本信息
		Resume resume = resumeDao.queryResume(resumeId);
		
		if(resume==null) {
			return ResultCode.error("简历不存在", resumeId);
		}
		ResumeVo vo = mapper.map(resume, ResumeVo.class);

		// 获取教育经历
		List<EducationalExperience> listedu = resumeDao.queryEduList(resumeId);

		// 保存教育经验
		if (!CollectionUtils.isEmpty(listedu)) {

			List<EducationalExperienceVo> eduList = new ArrayList<EducationalExperienceVo>();

			for (EducationalExperience ee : listedu) {
				EducationalExperienceVo eevo = mapper.map(ee, EducationalExperienceVo.class);
				eduList.add(eevo);
			}

			vo.setEduList(eduList);
		}

		// 获取工作经历
		List<WorkExperience> weList = resumeDao.queryWorkList(resumeId);

		// 保存教育经验
		if (!CollectionUtils.isEmpty(weList)) {

			List<WorkExperienceVo> workList = new ArrayList<WorkExperienceVo>();

			for (WorkExperience ee : weList) {
				WorkExperienceVo eevo = mapper.map(ee, WorkExperienceVo.class);
				workList.add(eevo);
			}

			vo.setWorkList(workList);
		}

		return ResultCode.success("获取简历成功", vo);
	}

	@Override
	public ResultData<Object> addWorkExperience(WorkExperienceVo workExperienceVo) {
		WorkExperience we = mapper.map(workExperienceVo, WorkExperience.class);
		we.setId(new RandomUtil().getRandomId());
		resumeDao.saveWorkExperience(we);
		return ResultCode.success("保存成功", workExperienceVo);
	}

	@Override
	public ResultData<Object> addeducationalExperience(EducationalExperienceVo educationalExperienceVo) {

		EducationalExperience we = mapper.map(educationalExperienceVo, EducationalExperience.class);
		we.setId(new RandomUtil().getRandomId());
		resumeDao.saveEducationalExperience(we);
		return ResultCode.success("保存成功", educationalExperienceVo);
	}

	@Override
	public ResultData<Object> updateWorkExperience(WorkExperienceVo workExperienceVo) {

		WorkExperience we = mapper.map(workExperienceVo, WorkExperience.class);
		resumeDao.updateWorkExperience(we);
		return ResultCode.success("更新成功", workExperienceVo);
	}

	@Override
	public ResultData<Object> updateEducationalExperience(EducationalExperienceVo educationalExperienceVo) {
		EducationalExperience we = mapper.map(educationalExperienceVo, EducationalExperience.class);
		resumeDao.updateEducationalExperience(we);
		return ResultCode.success("更新成功", educationalExperienceVo);
	}

	@Override
	public ResultData<Object> deleteEducationalExperience(String id) {

		resumeDao.deleteEducationalExperience(id);
		return ResultCode.success("删除成功", id);
	}

	@Override
	public ResultData<Object> deleteWorkExperience(String id) {
		resumeDao.deleteWorkExperience(id);
		return ResultCode.success("删除成功", id);
	}

	@Override
	public ResultData<Object> sendResume(String userId, String resumeId) {
		
		CompanyResume cr =new CompanyResume();
		cr.setUserId(userId);
		cr.setResumeId(resumeId);
		resumeDao.sendResume(cr);
		if(StringUtil.isEmptyString(cr.getId())) {
			return ResultCode.error("发送简历失败", cr);
		}
		return ResultCode.success("发送简历成功", cr);
	}

	@Override
	public Resume queryResumeByUserId(String userId) {
		return resumeDao.queryResumeByUserId(userId);
	}

	@Override
	public List<MenuVo> getUserMeuns(CompanyVo companyVo) {
		List<MenuVo> menus = new ArrayList<MenuVo>();
		/*MenuVo menu1 = new MenuVo();
		menu1.setMarkName("userManagement");
		menus.add(menu1);*/
		
		MenuVo menu2 = new MenuVo();
		menu2.setMarkName("accountManagement");
		menu2.setIcon("17.png");
		menus.add(menu2);
		
		MenuVo menu3 = new MenuVo();
		menu3.setMarkName("resumeManagement");
		menu3.setIcon("search.png");
		menus.add(menu3);
		
		MenuVo menu4 = new MenuVo();
		menu4.setMarkName("recruitManagement");
		menu4.setIcon("zhaopin.png");
		menus.add(menu4);
		
		MenuVo menu5 = new MenuVo();
		menu5.setMarkName("receiveManagement");
		menu5.setIcon("resume.png");
		menus.add(menu5);
		
		MenuVo menu6 = new MenuVo();
		menu6.setMarkName("loginLogManagement");
		menu6.setIcon("resume.png");
		menus.add(menu6);
		return menus;
	}

	@Override
	public ResultData<Object> sendResume(String userId, String resumeId, String companyId,String positionId) {
		
		// 判断是否重复投递
		CompanyResume  companyResume=resumeDao.queryCompanyResume(userId,resumeId,companyId,positionId);
		if(companyResume !=null) {
			return ResultCode.error("请勿重复投递", null);
		}
		CompanyResume cr =new CompanyResume();
		cr.setUserId(userId);
		cr.setResumeId(resumeId);
		cr.setCompanyId(companyId);
		cr.setPositionId(positionId);
		cr.setDeliverDate(new Date());
		User u =userDao.queryUserById(userId);
		if(u!=null) {
			cr.setSex(u.getSex());
		}
		
		
		resumeDao.sendResume(cr);
		if(StringUtil.isEmptyString(cr.getId())) {
			return ResultCode.error("投递简历失败", cr);
		}
		return ResultCode.success("投递简历成功", cr);
	}

	@Override
	public ResultData<Object> saveUserLabel(UserLabelVo labelVo) {
		
		UserLabel label =mapper.map(labelVo, UserLabel.class);
		label.setCreateDate(new Date());
		lableDao.saveUserLabel(label);
		return ResultCode.success("保存标签成功", label);
	}

	@Override
	public ResultData<Object> deleteLabel(String id) {
		lableDao.deleteLabel(id);
		return ResultCode.success("删除成功", null);
	}

	@Override
	public WeixinConfig queryUserIsAuth(String uniqueId) {
		return weixinConfigDao.querySessionKeyByUniqueId(uniqueId);
	}
}
